package cn.org.mrliu.ssm.common.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import cn.org.mrliu.ssm.common.web.vo.QueryParam;
import cn.org.mrliu.ssm.common.web.vo.ResponseMessage;
import cn.org.mrliu.ssm.constant.Global;

public abstract class BaseController {

	/**
	 * 处理页码
	 * 
	 * @param pageIndex
	 * @return
	 */
	protected Integer handlePageIndex(Integer pageIndex) {
		if (pageIndex == null || pageIndex == 0) {
			pageIndex = Global.DEFAULT_PAGE_INDEX;
		}
		return pageIndex;
	}

	/**
	 * 处理也大小
	 * 
	 * @param pageSize
	 * @return
	 */
	protected Integer handlePageSize(Integer pageSize) {
		if (pageSize == null || pageSize == 0) {
			pageSize = Global.DEFAULT_PAGE_SIZE;
		}
		return pageSize;
	}

	protected String[] handleIdString(String idString) {
		if (idString == null || idString.trim().length() < 1) {
			return null;
		}
		String[] ids = null;
		String splitString = null;
		if (idString.matches("^+?[1-9][0-9]*(_+?[1-9][0-9]*){0,}_?$")) {
			splitString = "_";
		} else if (idString.matches("^+?[1-9][0-9]*(-+?[1-9][0-9]*){0,}-?$")) {
			splitString = "-";
		} else if (idString.matches("^+?[1-9][0-9]*(,+?[1-9][0-9]*){0,},?$")) {
			splitString = ",";
		} else if (idString.matches("^+?[1-9][0-9]*(;+?[1-9][0-9]*){0,};?$")) {
			splitString = ";";
		}
		if (splitString != null) {
			ids = idString.split(splitString);
		}
		return ids;
	}

	protected Set<Long> handleIds(Long[] ids, String idString) throws Exception {
		return handleIds(ids, idString, null);
	}

	protected Set<Long> handleIds(Long[] ids, String idString, List<Long> idList) throws Exception {
		if ((ids == null || ids.length < 1) && (idString == null || idString.trim().length() < 1)
				&& (idList == null || idList.size() < 1)) {
			return null;
		}

		String[] idsStr = handleIdString(idString);
		Set<Long> idSet = new LinkedHashSet<Long>();
		if (ids != null && ids.length > 0) {
			for (Long long1 : ids) {
				if (long1 != null && long1 > 0) {
					idSet.add(long1);
				}
			}
		}
		if (idsStr != null && idsStr.length > 0) {
			for (String string : idsStr) {
				if (string != null && string.trim().length() > 0) {
					Long long1 = Long.parseLong(string);
					if (long1 > 0) {
						idSet.add(long1);
					}
				}
			}
		}
		if (idList != null && idList.size() > 0) {
			for (Long long1 : idList) {
				if (long1 != null && long1 > 0) {
					idSet.add(long1);
				}
			}
		}
		return idSet;

	}

	/**
	 * 获取验证错误的消息Map
	 * 
	 * @param result
	 * @return
	 */
	public Map<String, Object> getValidatorErrorMessageMap(BindingResult result) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ObjectError> objectErrors = result.getAllErrors();
		for (ObjectError objectError : objectErrors) {
			FieldError fieldError = (FieldError) objectError;
			String field = fieldError.getField();
			String errorMessage = fieldError.getDefaultMessage();
			map.put(field, errorMessage);
		}
		return map;
	}

	/**
	 * 获取验证错误的消息List
	 * 
	 * @param result
	 * @return
	 */
	public List<String> getValidatorErrorMessageList(BindingResult result) {
		List<ObjectError> objectErrors = result.getAllErrors();
		List<String> errorMessages = new ArrayList<String>();
		for (ObjectError objectError : objectErrors) {
			String errorMessage = objectError.getDefaultMessage();
			errorMessages.add(errorMessage);
		}
		return errorMessages;
	}

	/**
	 * 获取验证错误的消息对象
	 * 
	 * @param result
	 * @return
	 */
	public ResponseMessage getValidatorErrorResponseMessage(BindingResult result) {
		ResponseMessage responseMessage = new ResponseMessage();
		responseMessage.setSuccess(false);
		responseMessage.setMessage(getValidatorErrorMessageList(result));
		return responseMessage;

	}
	
	protected String saveTypeName;
	public String getSaveTypeName(Integer id) {
		if (id!=null) {//修改
			this.saveTypeName = "修改";
		}else{ // 添加
			this.saveTypeName = "添加";
		}
		return this.saveTypeName;
	}

}
